package com.example.hydropowermanage.controller;

import com.example.hydropowermanage.pojo.User123;
import com.example.hydropowermanage.pojo.UtilityUsage;
import com.example.hydropowermanage.service.LoginService;
import com.example.hydropowermanage.service.lmpl.UtilityUsageServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class WaterElectricityController {
    private final UtilityUsageServiceImpl waterElectricityService;
    private LoginService loginService;
    public WaterElectricityController(UtilityUsageServiceImpl waterElectricityService, LoginService loginService) {
        this.waterElectricityService = waterElectricityService;
        this.loginService = loginService;
    }
    //展示
    @GetMapping("/root/waterElectricity")
    public String getWaterElectricityInfo(@RequestParam("id") int userId, Model model) {
        model.addAttribute("userId", userId);
        // 使用服务获取当前 用户的水电信息
        List<UtilityUsage> all = waterElectricityService.findAllByUserId(userId);
        //计算月cost
        User123 user = loginService.findID(userId);
        if (user == null) {
            return "redirect:/root/index";
        }
        double waterPrice = user.getWaterPrice();
        double electricityPrice = user.getElectricityPrice();
        for (UtilityUsage usage : all) {
            double totalWaterUsage = usage.getTotalWaterUsage();
            double totalElectricityUsage = usage.getTotalElectricityUsage();
            double cost = totalWaterUsage * waterPrice + totalElectricityUsage * electricityPrice;
            usage.setCost(cost);
        }
        // 将信息添加到模型中，以便在视图中使用
        model.addAttribute("waterElectricityInfo", all);
        return "root/indexWater";
    }
    //删除
    @RequestMapping("/root/delWater")
    public String del(Model model, int id, ModelMap map){
        map.put("id",id);
        return "/root/delWater";
    }
    //删除操作
    @RequestMapping("/deleteWater")
    @ResponseBody
    public String deleteUser(UtilityUsage utilityUsage){
        System.out.println(utilityUsage);
        waterElectricityService.removeById(utilityUsage.getId());
        return "0";
    }
    //修改
    @RequestMapping("/root/editWater")
    public String edit(Model model,int id){
        System.out.println(id+"edit");
        UtilityUsage usage = waterElectricityService.findById(id);
        System.out.println(usage);
        model.addAttribute("usage",usage);
        return "/root/editWater";
    }
    //修改操作
    @RequestMapping("/updateWater")
    @ResponseBody
    public String updateBook(UtilityUsage usage){
        System.out.println(usage);
        boolean boo = waterElectricityService.checkIdUnique(usage.getId());
        if(boo){
            return "1";
        }
//        设置月份的格式yyyy-mm
        Date usageMonth = usage.getUsageMonth();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String formattedDate = formatter.format(usageMonth);
        usage.setUsageMonth(usageMonth);
        usage.setUpdatedAt(ScheduledTasks.calendar.getTime()); // 设置更新日期为当前时间
        double value = usage.getTotalWaterUsage() * usage.getPerWaterPrice() +
                usage.getTotalElectricityUsage() * usage.getPerElectricityPrice();
        usage.setCost(value);
        waterElectricityService.updateUtilityUsage(usage);
        //更新用户的水电费用
        User123 user123 = loginService.findID(usage.getUserId());
        if (user123 == null) {
            return "redirect:/root/indeWater";
        }
        System.out.println("edic" );
        System.out.println(user123);
        double waterConsumption = 0;
        double powerConsumption = 0;
        double totalCost = 0;
        List<UtilityUsage> all = waterElectricityService.findAllByUserId(usage.getUserId());
        for (UtilityUsage utilityUsage:all) {
            waterConsumption+= utilityUsage.getTotalWaterUsage();
            powerConsumption+= utilityUsage.getTotalElectricityUsage();
            totalCost+= utilityUsage.getCost();
        }
        user123.setWaterConsumption(waterConsumption);
        user123.setPowerConsumption(powerConsumption);
        user123.setTotalCost(totalCost);
        loginService.updateUser(user123);
        System.out.println(user123);
        System.out.println(loginService.findID(usage.getUserId()));

        return "0";
    }


}

